# -*- coding: utf-8 -*-

try:
    from spider.site_index import BaseSiteIndex
except ImportError:
    from site_index import BaseSiteIndex
from PalmWebsite import app
import re


class BaiduSiteLink(BaseSiteIndex):
    SEARCH_URL = "http://www.baidu.com/s?wd=domain:%s"

    def parser(self):
        soup = self._soup()
        span = soup.find("span", attrs={"class": "nums"})
        app.logger.debug(span)
        nums = self.str2int(span.text)
        return nums


class GoogleSiteLink(BaseSiteIndex):
    SEARCH_URL = "http://www.google.com.hk/search?q=link:%s"

    def parser(self):
        soup = self._soup()
        div = soup.find("div", attrs={"id": "resultStats"})
        if div:
            app.logger.debug(div)
            text = re.compile(ur"约 [0-9,]+ 条").findall(div.text)
            nums = self.str2int(text[0])
            return nums
        return 0


class BingSiteLink(BaseSiteIndex):
    SEARCH_URL = 'http://cn.bing.com/search?q="%s"'

    def parser(self):
        soup = self._soup()
        span = soup.find("span", attrs={"class": "sb_count"})
        if span:
            app.logger.debug(span)
            nums = self.str2int(span.text)
            return nums
        return 0


class SogouSiteLink(BaseSiteIndex):
    SEARCH_URL = 'http://www.sogou.com/web?query="%s"'

    def parser(self):
        soup = self._soup()
        span = soup.find("span", attrs={"id": "scd_num"})
        if span:
            app.logger.debug(span)
            nums = self.str2int(span.text)
            return nums
        return 0


class SoSiteLink(BaseSiteIndex):
    SEARCH_URL = 'http://www.so.com/s?q="%s"'

    def parser(self):
        soup = self._soup()
        span = soup.find("span", attrs={"class": "nums"})
        if span:
            app.logger.debug(span)
            text = re.compile(ur"[0-9,]+").findall(span.text)
            nums = self.str2int(text[0])
            return nums
        return 0



if __name__ == '__main__':
    baidu = BaiduSiteLink("qq.com")
    google = GoogleSiteLink("qq.com")
    bing = BingSiteLink("qq.com")
    sogou = SogouSiteLink("qq.com")
    so = SoSiteLink("qq.com")
    print so.get_nums()
